package com.mazaiting.easy.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mazaiting.easy.app.BaseApplication;
import com.mazaiting.easy.base.interfaces.IView;
import com.mazaiting.easy.base.mvp.IBaseView;
import com.mazaiting.easy.base.presenter.BasePresenter;
import com.trello.rxlifecycle3.LifecycleProvider;
import com.trello.rxlifecycle3.components.support.RxFragment;

import javax.inject.Inject;


/**
 * Fragment基类
 * @author mazaiting
 * @date 2018/2/5
 */
public abstract class BaseFragment<T extends BasePresenter>
        extends RxFragment implements IView, IBaseView{
    /**上下文*/
    private Context mContext;
    /**当前View*/
    private View mRootView;
    /**主持人*/
    @Nullable @Inject T mPresenter;
    /**判断是否加载过数据*/
    boolean hasFetchData = false;

    @Nullable @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 判断View是否为空
        if (null != mRootView) {
            // 不为空时获取它的父布局
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            // 判断父布局是否为空
            if (null != parent) {
                // 如果不为空，则移除当前布局
                parent.removeView(mRootView);
            }
        } else {
            // 为空时创建View
            mRootView = inflater.inflate(getContentLayout(), container, false);
        }
        // 设置上下文
        mContext = mRootView.getContext();
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 注入当前Fragment
        inject(BaseApplication.getInstance().getApplicationComponent());
        // 视图与Presenter绑定
        attachView();
        // 绑定View
        bindView(view, savedInstanceState);
        // 判断是否懒加载数据
        if (!isLazyFetchData()) {
            // 初始化数据
            initData();
            // 设置为已经加载数据
            hasFetchData = true;
        }
    }

    /**
     * 判断是否是懒加载, 默认为false, 即为非懒加载
     * @return true, 是懒加载；false, 不是懒加载
     */
    protected boolean isLazyFetchData() {
        return false;
    }

    /**
     * 绑定布局
     */
    private void attachView() {
        if (null == mPresenter) {
            throw new RuntimeException("Presenter 为空, 请使用注入 Presenter 的构造方法.");
        }
        mPresenter.attachView(this);
    }

    /**
     * 获取当前布局
     * @return 当前布局
     */
    @Nullable @Override
    public View getView() {
        return mRootView;
    }

    /**
     * 获取上下文
     * @return 上下文
     */
    public Context getContext() { return mContext; }

    /**
     * 获取主持人
     * @return 主持人
     */
    @Nullable @Override public T getPresenter() {
        return mPresenter;
    }

    @Override
    public LifecycleProvider getLifecycleProvider() {
        return this;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 判断是否为空
        if (null != mPresenter) {
            // 不为空，则使Presenter与View解除绑定
            mPresenter.detachView();
        }
    }
}
